define(["jquery", "easy-admin", "echarts", "echarts-theme",], function ($, ea, echarts) {
    var table = layui.table;
    $(function () {
        $("#time").text(time());
    })
    function time() {
        var now = new Date();

        var year = now.getFullYear(); //得到年份

        var month = now.getMonth() + 1;//得到月份

        var date = now.getDate();//得到日期

        var hour = now.getHours();//得到小时数

        var minute = now.getMinutes();//得到分钟数

        var second = now.getSeconds();//得到秒数

        return year + "-" + addZero(month) + "-" + addZero(date) + " " + addZero(hour) + ":" + addZero(minute) + ":" + addZero(second);

    }

    function addZero(num) {
        return num.toString().padStart(2, '0');
    }
    return {
        index: function () {
            setInterval(() => {
                $("#time").text(time());
            }, 1000);

            /**
            * 报表功能
            */
            $(function () {
                $('#layui-version').text('v' + layui.v);
                let echartsRecords = echarts.init(document.getElementById('echarts-records'), 'walden');
                let optionRecords = chartOptions();
                optionRecords.title.text = "年度出勤";
                optionRecords.series = [
                    {
                        name: '出勤',
                        type: 'line',
                        label: {
                            show: true,
                            position: 'top'
                        },
                        data: []
                    }
                ];
                echartsRecords.setOption(optionRecords);

                let leaveEchartsRecords = echarts.init(document.getElementById('leave-echarts-records'), 'walden');
                let optionRecords2 = chartOptions();
                optionRecords2.title.text = "年度请假";
                optionRecords2.series = [
                    {
                        name: '请假',
                        type: 'line',
                        itemStyle: {
                            color: '#a80000' // 改变折线颜色
                        },
                        label: {
                            show: true,
                            position: 'top'
                        },
                        data: []
                    }
                ];
                leaveEchartsRecords.setOption(optionRecords2);

                window.addEventListener("resize", function () {
                    echartsRecords.resize();
                });

                $.ajax({
                    url: 'attendanceStatistics',
                    success: function (response) {
                        var optionRecords = {
                            series: response
                        };
                        echartsRecords.setOption(optionRecords);
                        leaveEchartsRecords.setOption(optionRecords);
                    }
                })

                function chartOptions() {
                    let optionRecords = {
                        title: {
                            text: '年度出勤'
                        },
                        tooltip: {
                            trigger: 'axis'
                        },
                        grid: {
                            left: '3%',
                            right: '4%',
                            bottom: '3%',
                            containLabel: true
                        },
                        toolbox: {
                            feature: {
                                saveAsImage: {}
                            }
                        },
                        xAxis: {
                            type: 'category',
                            boundaryGap: false,
                            data: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月']
                        },
                        yAxis: {
                            type: 'value'
                        }
                    };
                    return optionRecords;
                }
                $('#attendance').on('click', function () {
                    var index = layer.open({
                        type: 2,
                        title: '添加出勤',
                        shadeClose: true,
                        maxmin: true, //开启最大化最小化按钮
                        content: '/admin/work.attendance/add'
                    });
                    layer.full(index);
                })
            })
            ea.listen();
        },
        attendance: function () {

            // 创建渲染实例
            table.render({
                elem: '#attendanceTable',
                url: '/admin/work.attendance/index',
                page: true,
                toolbar: '#toolbar',
                cols: [[
                    { field: 'date', title: '出勤日期', width: 200, search: "range-date", searchOp: 'date', timeType: 'date' },
                    {
                        field: 'state', title: '出勤状态', width: 150, templet: function (d) {
                            if (d.state === 1) {
                                return ' <button type="button" class="layui-btn layui-btn-sm">上班</button>'
                            } else if (d.state === 0) {
                                return ' <button type="button" class="layui-btn layui-bg-orange layui-btn-sm">休息</button>'
                            } else {
                                return ' <button type="button" class="layui-btn layui-bg-red layui-btn-sm">请假</button>'
                            }
                        }, selectList: { '-1': '请假', '0': '休息', '1': '上班' }
                    },
                ]]
            });

            table.on('toolbar(attendanceTable)', function (obj) {
                var index = layer.open({
                    type: 2,
                    title: '添加出勤',
                    shadeClose: true,
                    maxmin: true, //开启最大化最小化按钮
                    content: '/admin/work.attendance/add'
                });
                // 设置弹层最大化
                layer.full(index);
            });
            ea.listen();
        },
    };
});